package com.myframe.www;

import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.myframe.www.base.BaseActivity;
import com.myframe.www.model.local.UserBean;
import com.myframe.www.model.local.UserCenter;
import com.myframe.www.model.remote.PassportService;
import com.myframe.www.model.remote.bean.LoginBean;
import com.myframe.www.model.remote.param.LoginParam;
import com.myframe.www.net.FailureBean;
import com.myframe.www.net.HttpHandler;
import com.myframe.www.net.ParamFactory;
import com.myframe.www.net.ServerField;
import com.myframe.www.net.ServerTaskObserver;
import com.yong.android.framework.utils.DisposableUtils;
import com.yong.android.framework.utils.EasyToast;
import com.yong.android.framework.utils.log.SLog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {
    private UserCenter.LoginBeanDao loginBeanDao;
    private UserCenter.UserBeanDao userBeanDao;
    @Bind(R.id.et_phone)
    EditText mEtPhone;
    @Bind(R.id.iv_phone_close)
    ImageView mIvPhoneClose;
    @Bind(R.id.et_verification_code)
    EditText mEtVerificationCode;
    @Bind(R.id.action_code)
    Button mActionCode;
    @Bind(R.id.action_login)
    TextView mActionLogin;
    private Disposable mDisposable;

    @Override
    protected void initToolbar() {
//        mToolbar.setTitle("登录");
//        mToolbar.setBackIconVisible(false);

    }

    @Override
    protected int getContentViewLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(BuildConfig.DEBUG==true){
            mEtPhone.setText("15980805071");
            mEtVerificationCode.setText("502");
        }
        loginBeanDao = new UserCenter.LoginBeanDao(this);
        userBeanDao = new UserCenter.UserBeanDao(this);
    }



    public String getPhone(){
       return mEtPhone.getText().toString();
    }

    public String getPhoneCode(){
        return mEtVerificationCode.getText().toString();
    }


    @OnClick({R.id.action_code, R.id.action_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.action_code:
                break;
            case R.id.action_login:
                login();
                break;
        }
    }

    private void login() {
        if(TextUtils.isEmpty(getPhone())){
            EasyToast.showToast(getApplicationContext(),"请输入手机号码");
        }else if(TextUtils.isEmpty(getPhoneCode())){
            EasyToast.showToast(getApplicationContext(),"请输入手机验证码");
        }
        HttpHandler.create(PassportService.class)
                .login(ParamFactory.generationParamBody(new LoginParam(getPhone(),getPhoneCode())))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ServerTaskObserver<LoginBean>() {

                    @Override
                    public void onSuccess(LoginBean value) {
                        loginBeanDao.save(value);
                        saveUserBean();
                    }
                    @Override
                    public void onFailure(FailureBean failureBean) {
                        SLog.e(failureBean.failureMessage(getApplicationContext()));
                    }
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                });


    }

    private void saveUserBean() {
        HttpHandler.create(PassportService.class)
                .getUserInfo(ServerField.getAuthorizationHeader())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ServerTaskObserver<UserBean>() {
                    @Override
                    public void onSuccess(UserBean value) {
                        userBeanDao.save(value);
                        LoginBean loginBean = UserCenter.getLoginBean();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    @Override
                    public void onFailure(FailureBean failureBean) {
                        SLog.e(failureBean.failureMessage(getApplicationContext()));
                    }
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DisposableUtils.dispose(mDisposable);
    }
}
